﻿using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Model;
using EasyXaf.ImageViewers;

namespace EasyXaf.Blazor.ImageViewers;

public class ImageViewerEditor : PropertyEditor
{
    public ImageViewerEditor(Type objectType, IModelMemberViewItem model)
        : base(objectType, model)
    {
    }

    protected override object CreateControlCore()
    {
        var model = new ImageViewerModel
        {
            ImageUrl = PropertyValue as string
        };

        if (Model is IModelImageViewer imageViewerModel)
        {
            model.Width = imageViewerModel.ImageViewerWidth;
            model.Height = imageViewerModel.ImageViewerHeight;
        }

        return new ImageViewerAdapter(model);
    }

    protected override object GetControlValueCore()
    {
        return ((ImageViewerAdapter)Control).Model.ImageUrl;
    }

    protected override void ReadValueCore()
    {
        ((ImageViewerAdapter)Control).Model.ImageUrl = PropertyValue as string;
    }
}
